package mapper

import (
	"newsletter-subs/pkg/db"
	"newsletter-subs/pkg/model"
)

// Mysql mapper
type MapperSubscription struct{}

var Subscription = &MapperSubscription{}

// create subscription
func (m *MapperSubscription) Create(sub *model.Subscription) (id uint32, err error) {
	r := db.DB.Create(sub)
	return sub.Id, r.Error
}

// delete subscription
func (m *MapperSubscription) Delete(id uint32) (err error) {
	r := db.DB.Delete(&model.Subscription{}, id)
	return r.Error
}

func (m *MapperSubscription) Update(sub *model.Subscription) (err error) {
	r := db.DB.Model(model.Subscription{}).Where("id=?", sub.Id).Updates(sub)
	return r.Error
}

// FindById
func (m *MapperSubscription) FindById(index uint32) (sub model.Subscription) {
	db.DB.First(&sub, index)
	return
}

// FindAll
func (m *MapperSubscription) FindAll() (subs []model.Subscription) {
	db.DB.Find(&subs)
	return
}
